<?php

class Html_Element_Date extends Html_Element_Field
{
	protected $_meta = array('render' => 'group', );
	protected $_attrib = array('class' => 'text date');

	public function beforeRender()
	{
		$y = intval(date('Y'));
		$m = intval(date('m'));
		$d = intval(date('d'));
		$optionYears = array('' => 'year');
		$dayOptions = array('' => 'day');
		$optionMonths = array(
				'' => 'month',
				1 => 1,
				2 => 2,
				3 => 3,
				4 => 4,
				5 => 5,
				6 => 6,
				7 => 7,
				8 => 8,
				9 => 9,
				10 => 10,
				11 => 11,
				12 => 12
		);

		for ($i = 0; $i < 30; ++$i)
		{
			$optionYears[$y - $i] = $y - $i;
		}

		for ($i = 1; $i < 32; ++$i)
		{
			$dayOptions[$i] = $i;
		}

		$name = $this -> getName();

		$this -> addChild('@select', $name . '[year]', array('multiOptions' => $optionYears, ));

		$this -> addChild('@select', $name . '[month]', array('multiOptions' => $optionMonths));

		$this -> addChild('@select', $name . '[day]', array('multiOptions' => $dayOptions));
	}

	public function isValid($value)
	{
		if (is_string($value))
		{

		}else
		if (is_array($value))
		{
			$date = sprintf('%4.0d-%2.0d-%2.0d', $value['year'], $value['month'], $value['day']);
			echo $date;
			$this -> setValue($date);
		}
	}

}
